package cn.ekgc.system.shiro;

import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultSecurityManager securityManager){

        // 定义拦截规则   哪些接口拥有哪些角色或者权限才能访问
        System.out.println("注册过滤器");
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        // 定义map集合
        Map<String,String> filters = new HashMap<>();
        filters.put("/login","anno");// 所有人可以访问
        filters.put("/login1","anno");// 所有人可以访问
        filters.put("/test","authc");//  认证后可以访问
        filters.put("/test3","perms['system:user:*']");//  拥有权限后可以访问
        filters.put("/test4","port");//  指定端口后可以访问
        filters.put("/test5","roles");//  拥有指定角色后可以访问
        filters.put("/test6","user");//  拥有指定用户后可以访问
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filters);
        return shiroFilterFactoryBean;
    }

    @Bean
    public DefaultSecurityManager securityManager(MyrealM myrealM){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(myrealM);
        return defaultWebSecurityManager;
    }

    @Bean
    public MyrealM myrealM(){
        return new MyrealM();
    }
}